package controller;

import model.Teacher;
import service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class TeacherController{
    private TeacherService teacherService;

    public void setTeacherService(TeacherService teacherService){
        this.teacherService=teacherService;
    }

    @RequestMapping("/getAll_teacher")
    public ModelAndView getAll() throws Exception{
        ArrayList<Teacher> teacher=teacherService.getAllTeachers();
        ModelAndView modelAndView=new ModelAndView("show_teacher");
        modelAndView.addObject("Teachers",teacher);
        return modelAndView;
    }

    @RequestMapping("/delete_teacher")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        String tno= request.getParameter("tno");
        boolean success=teacherService.deleteTeacher(tno);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_teacher.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_teacher.jsp";
        }

    }

    @RequestMapping("/get_teacher")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String tname= request.getParameter("tname");
        ArrayList<Teacher> teacher=teacherService.getTeacher(tname);
        ModelAndView modelAndView=new ModelAndView("show_teacher1");
        modelAndView.addObject("Teacher",teacher);
        return modelAndView;
    }

    @RequestMapping("/edit_teacher")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String tno= request.getParameter("tno");
        ArrayList<Teacher> teacher=teacherService.getTeacher(tno);
        ModelAndView modelAndView=new ModelAndView("show_teacher");
        modelAndView.addObject("Teacher",teacher);
        return modelAndView;
    }

    @RequestMapping("/doEdit_teacher")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String tname,String tSex,String prof,String majorId,RedirectAttributes redirectAttributes)throws Exception{
        String tno= request.getParameter("tno");
        Teacher teacher=new Teacher();
        teacher.setTno(tno);
        teacher.settName(tname);
        teacher.settSex(tSex);
        teacher.setProf(prof);
        teacher.setMajorId(majorId);
        boolean succ=teacherService.editTeacher(teacher);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_teacher.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_teacher.jsp";
        }

    }

    @RequestMapping("/add_teacher")
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView("show_teacher");
        return modelAndView;

    }

    @RequestMapping("/doAdd_teacher")
    public String doAdd(String tno,String tname,String tSex,
                        String prof,String majorId,RedirectAttributes redirectAttributes)throws Exception{
        Teacher teacher=new Teacher();
        teacher.setTno(tno);
        teacher.settName(tname);
        teacher.settSex(tSex);
        teacher.setProf(prof);
        teacher.setMajorId(majorId);
        boolean succ=teacherService.addTeacher(teacher);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_teacher.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_teacher.jsp";
        }
    }
}
